package suanfa.lianxi;

public class Studentlink {
    public static class node{
        public node next;
        public String name;
        public int point;

        public node(String name,int p){
            this.name=name;
            this.point=p;
        }

        public node(){}
    }
    public node head;

    public void createlink(){//创建节点
        node n1=new node("同学1",60);
        node n2=new node("同学2",100);
        node n3=new node("同学3",80);
        head=n1;
        n1.next=n2;
        n2.next=n3;
    }
    public void find(){
        node tem=head;
        //记录最高和最低
        String max="";
        String min="";
        while(tem.next!=null){//遍历节点
            if(tem.point>=tem.next.point){
                max=tem.name;
            }
            else{
                max=tem.next.name;
            }
            if(tem.point<=tem.next.point){
                min=tem.name;
            }
            else{
                min=tem.next.name;
            }
            tem=tem.next;
        }
        System.out.println("最高分的:"+max);
        System.out.println("最低分的:"+min);

    }
    public node delete(String name){
        node tem=head;
        if(head==null){//为空直接返回
            return null;
        }
        if(head.name.equals(name)){//处理头结点
            node dele=head;
            head=head.next;
            System.out.println("已删除"+name);
            return dele;
        }
        while(tem.next!=null){
            if(tem.next.name.equals(name)){
                node dele=tem.next;
                tem.next=tem.next.next;
                System.out.println("已删除"+tem.next.name);
                return dele;
            }
            tem=tem.next;
        }

        return null;
    }
    public void  paixu(){//冒泡从大到小
        if(head==null) return;
           //1,3,4,6,2
           boolean s=true;
           while(s){
               node pre=null;
               node cur=head;
               s=false;

               while(cur!=null&&cur.next!=null) {
                   node nextnode = cur.next;
                   if (cur.point < nextnode.point) {
                       if(pre==null){
                        head=nextnode;
                       }else{
                           pre.next=nextnode;
                       }
                       cur.next = nextnode.next;
                        nextnode.next=cur;
                        pre=nextnode;
                        s=true;
                   }
                   else{
                       pre=cur;
                       cur=cur.next;
                   }
               }

           }
           node i=head;
           while(i!=null){
               System.out.println(i.point);
               i=i.next;
           }
    }
    public void  addlast(String name,int p){//尾插法
            node newnode=new node();

            newnode.name=name;
            newnode.point=p;
            if(head==null){
                head=newnode;
            }else {
                node tem=head;
                while (tem.next != null) {//遍历节点
                    tem = tem.next;
                }
                tem.next=newnode;//将临时节点的下一个指向新节点
            }
    }

    public static void main(String[] args) {
        Studentlink l=new Studentlink();
        l.createlink();
        l.addlast("同学4",70);
        l.delete("同学1");
        l.find();
        System.out.println("----------");
        l.paixu();


    }
}
